package com.ymttest.datadriver;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

public class ImportDataFromExcelImpl implements ImportData{
	
	private List<Map<String,String>> allData ;
	private Map<String, String> testData;
	
	
	
	private int caseId;
	private String caseSummary;
	private String scenario;
	private String scenarioSummary;
	private JSONObject scenarioModel;
	private Map<String, String> modelUpdateMap = new HashMap<String, String>();
	private Map<String, String> caseAssert = new HashMap<String, String>();
	
	private Map<Integer,ImportData> _dependCaseIds = new HashMap<Integer,ImportData>();
	private Map<Integer, List<String>> _dependCaseIdsVal =new HashMap<Integer, List<String>>();
	private String _api;
	private String _host;

	
	
	private void initData(){
		caseId = Integer.parseInt(testData.getOrDefault(ExcelTestDataTitle.id,"-1"));
		
		caseSummary = testData.getOrDefault(ExcelTestDataTitle.caseSummary,"");
		
		scenario =  testData.getOrDefault(ExcelTestDataTitle.scenario,"");
		
		scenarioSummary = testData.getOrDefault(ExcelTestDataTitle.scenarioSummary,"");
		
		scenarioModel = ThirdPartData.getScenarioModelByExcel(scenario,scenarioSummary,testData.getOrDefault(ExcelTestDataTitle.scenarioModel,""));

		modelUpdateMap = StringToMap(testData.getOrDefault(ExcelTestDataTitle.updateMap,""));
		
		caseAssert = StringToMap(testData.getOrDefault(ExcelTestDataTitle.caseAssert,""));
		
		_api = ThirdPartData.getApiByExcel(scenarioSummary);
		
		_host = ThirdPartData.getHostByExcel(scenario);
		
		
		//_dependCaseIds & _dependCaseIdsVal
		for(String key : modelUpdateMap.keySet()){
			Object V = modelUpdateMap.get(key);
			if (V.toString().contains(".")){
				String[] args = V.toString().split("\\.");
				Integer beforeId = Integer.parseInt(args[0]);
				String pathKey = args[1];
				
				if (_dependCaseIdsVal.keySet().contains(beforeId)){
					_dependCaseIdsVal.get(beforeId).add(pathKey);
				}else{
					List<String> keysList = new ArrayList<String>();
					keysList.add(pathKey);
					_dependCaseIdsVal.put(beforeId, keysList);
				}
				
				for(Map<String, String> rowMap : allData){
					if (beforeId.toString().equals(rowMap.get(ExcelTestDataTitle.id).toString())){
						_dependCaseIds.put(beforeId, new ImportDataFromExcelImpl(allData,rowMap));
						break;
					}
				}
			}	
		}
	
	
	}
	
	public ImportDataFromExcelImpl(List<Map<String,String>> allData, Map<String, String>testData){
		this.allData = allData;
		this.testData = testData;
		initData();
	}
	
	public ImportDataFromExcelImpl(File excelFile,int sheetIndex,int dataRow){
		List<Map<String,String>> mapList = ExcelOperate.getDataAsMap(excelFile,sheetIndex);
		this.allData = mapList;
		this.testData = mapList.get(dataRow);
		initData();
	}
	
	@Override
	public int getCaseId() {
		return caseId;
	}

	@Override
	public String getCaseSummary() {
		return caseSummary;
	}

	@Override
	public String getScenario() {
		return scenario;
	}

	@Override
	public String getScenarioSummary() {
		return scenarioSummary;
	}

	@Override
	public JSONObject getScenarioModel() {
		return scenarioModel;
	}

	@Override
	public Map<String, String> getModelUpdateMap() {
		return modelUpdateMap;
	}

	@Override
	public Map<String, String> getCaseAssert() {
		return caseAssert;
	}

	@Override
	public  Map<Integer,ImportData> _getDependCaseIds() {
		return _dependCaseIds;
	}
	
	@Override
	public Map<Integer, List<String>> _getDependCaseIdsVal() {
		// TODO Auto-generated method stub
		return _dependCaseIdsVal;
	}
	
	@Override
	public String _getApi() {
		return _api;
	}
	
	@Override
	public String _getHost() {
		return _host;
	}
	
	/***
	 * a=b;c=d;T=1
	 * @param s
	 * @return
	 */
	private Map<String, String> StringToMap(String s){
		Map<String, String> map  =new HashMap<String,String>();
		String[] pair  = s.split(";");
		if (pair.length>0 && pair[0].length()>0){
			for (int i=0;i<pair.length;i++){
				String[] keyval = pair[i].split("=");
				map.put(keyval[0].trim(), keyval[1].trim());
			}
		}
		return map;
	}











	


}
